import 'package:flutter/material.dart';
import './pages/home.dart';
import './pages/contact.dart';
import './pages/discovery.dart';
import './pages/profile.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    print("INFO: CALL MyApp.build");
    return MaterialApp(
      title: 'Flutter Demo  123',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey
      ),

      // 命名路由表
      // routes: {
      //     "word_page": (context) => ContactWidget(),
      //     "tapbox_a": (context) => TapBoxA()
      //   },

      home: MyHomePage(title: '微信'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() {
    print("INFO: CALL MyHomePag.createState()");
    return _MyHomePageState();
  }
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  int _currentIndex = 0;
  
  final List<Widget> _children = [HomeWdiget(), ContactWidget(), DiscoveryWidget(), ProfileWidget()];

  final List<BottomNavigationBarItem> _list = <BottomNavigationBarItem> [
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      title: Text('微信'),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.book),
      title: Text('通讯录'),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.music_video),
      title: Text('发现'),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.people),
      title: Text('我'),
    )
  ];

  void incrementCount() {
    setState(() {
     _counter ++; 
    });
  }

  void onTaped(int index) {
    setState(() {
     _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    print("INFO: call _MyHomePageState.build");
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
        actions: <Widget>[
          Padding(
            padding: EdgeInsets.only(right: 20),
            child: Row(
              children: <Widget>[
                Icon(Icons.search, color: Colors.white,),
                Icon(Icons.add, color: Colors.white,)
              ],
            ),
          )
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTaped,
        currentIndex: _currentIndex,
        items: _list,
      ),
      body: _children[_currentIndex]
       // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
